<?php

namespace bc\model\wc;

use bc\ar\MActiveRecord;
use bc\model\shop\ShopUserModel;
use bc\wx\User;
use yii\web\Session;
use bc\model\UserModel as zUser;
use bc\model\OrderModel;

class BookingModel extends MActiveRecord
{
    const TOTAL_PRICE = 2400.00;
    const GR_DEDUCTION = 300.00;
    const DR_DEDUCTION = 200.00;
    /**
     * 定义数据表
     */
    public static function tableName()
    {
        return '{{%wc_booking}}';
    }

    

    /**
     * bc_user表实体名
     */
    protected function getUserModelName()
    {
    	return zUser::getTableSchema()->name;
    }
    
    /*
     * 获取用户的基本预约信息
     *
     * */
    public function getUserInfo($openid)
    {
        $userModel = new zUser();
        $whereUserModel =  array('wxid'=>$openid);
        $userModelRow = $userModel->getRow($whereUserModel);

        $bookingModel = new BookingModel();
        $whereUserModel =  array('user_id'=>$userModelRow['id']);
        $bookingModelRow = $bookingModel->getRow($whereUserModel);
        $userModelRow['b_id']=$bookingModelRow['id'];
        $userModelRow['user_id']=$bookingModelRow['user_id'];
        $userModelRow['childbirth_date']=$bookingModelRow['childbirth_date'];
        $userModelRow['bpoint_id']=$bookingModelRow['bpoint_id'];
        $userModelRow['doctor_id']=$bookingModelRow['doctor_id'];
        $userModelRow['status']=$bookingModelRow['status'];
        $userModelRow['report_url']=$bookingModelRow['report_url'];
        $userModelRow['is_invalid']=$bookingModelRow['is_invalid'];

        //获取order表是否有这个订单信息
        $orderModel = new OrderModel();
        $orderData=$orderModel->getRowOne(['order_no'=>$bookingModelRow['order_no']]);
        $userModelRow['order_id']=$orderData['order_id'];
        return $userModelRow;
    }

    /*
     * 根据手机或查询用户预约情况
     * */
    public function getUserInfoFromaPhone($mobile)
    {
        $dao = new MActiveRecord();
        //$sql = "select a.id as aid,b.id as bid,c.id as cid,d.id as did,a.*,b.*,c.*,d.* from bc_user a ,bc_wc_booking b,bc_wc_doctor c,bc_wc_hospital d ,bc_user e where a.id=b.user_id and b.status in(2,3,4) and b.doctor_id = c.id and c.user_id = e.id and c.hospital_id=d.id and a.mobile='{$mobile}';";
        $sql = "select a.id as aid ,b.id as bid,b.order_id as border_id,c.id as cid,f.id as fid,d.id as did,f.name as fname,a.*,b.*,c.*,d.*,f.* from  bc_user a , bc_wc_booking b left join bc_wc_doctor c on b.doctor_id = c.id left join bc_wc_hospital d on c.hospital_id=d.id left join bc_user e on c.user_id = e.id left join bc_wc_bpoint f on f.id = b.bpoint_id where a.id=b.user_id and b.status in (2,3,4) and  a.mobile='{$mobile}';";
        $row = $dao->getDb()->createCommand($sql)->queryOne();
        return $row;
    }

    /*
     * 根据表ID获取用户预约数据
     * */
    public function getUserInfoFromaID($id)
    {
        $dao = new MActiveRecord();
        //$sql = "select a.id as aid,b.id as bid,c.id as cid,d.id as did,a.*,b.*,c.*,d.* from bc_user a ,bc_wc_booking b,bc_wc_doctor c,bc_wc_hospital d ,bc_user e where  a.id=b.user_id and b.status in(2,3,4) and b.doctor_id = c.id and c.user_id = e.id and c.hospital_id=d.id and  b.id='{$id}';";
        $sql = "select a.id as aid ,b.id as bid,b.order_id as border_id,c.id as cid,d.id as did,f.id as fid,f.name as fname,a.*,b.*,c.*,d.*,f.* from  bc_user a , bc_wc_booking b left join bc_wc_doctor c on b.doctor_id = c.id left join bc_wc_hospital d on c.hospital_id=d.id left join bc_user e on c.user_id = e.id left join bc_wc_bpoint f on f.id = b.bpoint_id  where a.id=b.user_id and b.id='{$id}';";
        $row = $dao->getDb()->createCommand($sql)->queryOne();
        return $row;
    }
    
    /**
     * @param array $params
	 * @param string $page
	 * @param string $pageSize
     */
    public function getList($params, $page = null, $pageSize = null)
    {
    	$ar = $this->find ()->select ( $this->columns )->leftJoin ( $this->getUserModelName(), self::getTableSchema ()->name . '.user_id=' . $this->getUserModelName() . '.id' );
    	if ($params)
    	{
    		$ar->where ( $params );
    	}
    	if ($page && $pageSize)
    	{
    		$offset = $this->getOffset ( $page, $pageSize );
    		$ar->offset ( $offset )->limit ( $pageSize );
    	}
    	
		return $ar->asArray ()->all ();
    }
    
    
    /**
     *
     */
    public function getTotal($condition)
    {
    	if(!empty($condition))
    	{
    		$ar = $this->find ()->select ( $this->columns )->leftJoin ( $this->getUserModelName(), self::getTableSchema ()->name . '.user_id=' . $this->getUserModelName() . '.id' );
    		if ($condition)
    		{
    			$ar->where ( $condition );
    		}
    		return $ar->count();
    	}
    	else
    		return 0;
    }
    
    /**
     * 
     * @param int $id
     */
    public function getRowById($id)
    {
    	$ar = $this->find ()->select ( $this->columns )->leftJoin ( $this->getUserModelName(), self::getTableSchema ()->name . '.user_id=' . $this->getUserModelName() . '.id' );
    	$ar->where([self::getTableSchema ()->name . '.' . $this->autoId => $id]);
    	return $ar->asArray ()->one();
    }

    /*
     * 根据order_id获取抵扣金额
     * */
    public function getDeduction($order_id)
    {
        //得到用户的openid,$orderData[''];
        $orderM = new OrderModel();
        $orderData = $orderM->getRowById($order_id);
        $shopUserM = new ShopUserModel();
        $shopUserData = $shopUserM->getRowById($orderData['user_id']);
        if(!empty($orderData) && !empty($shopUserData) ){

            //查看是否为医生推荐$shopUserData['wxid']
            $spreadM = new SpreadModel();
            $spreadData=$spreadM ->getRow(['user_penid'=>$shopUserData['wxid'],'spread_status'=>2]);
            if(!empty($spreadData))
            {
                return self::DR_DEDUCTION;
            }
        }
        //查不到数据，不属于任何人，抵扣200
        return self::GR_DEDUCTION;
    }


}